package Service;

import Service.PriceData;
import facecat.topin.service.*;
import facecat.topin.core.*;
import java.util.*;

public class PriceDataServiceClient extends FCClientService{
    public PriceDataServiceClient(){
        setServiceID(SERVICEID);
    }
    public static int SERVICEID = 10004;
    
    //注册推送
    public static int FUNCTION_SUBCODES = 0;

    //取消推送
    public int FUNCTION_UNSUBCODES = 1;

    //最新数据
    public static int FUNCTION_NEWDATA = 2;

    private int m_socketID;

    /*
     * 获取套接字
     */
    public int getSocketID() {
        return m_socketID;
    }
    
    /*
     * 设置套接字
     */
    public void setSocketID(int value) {
        m_socketID = value;
    }

    /*
     * 注册代码
     *@param requestID 请求ID
     */
    public void subCodes(int requestID, String codes) {
        FCBinary bw = new FCBinary();
       try{
            bw.writeString(codes);
            byte[] bytes = bw.getBytes();
            int ret = send(new FCMessage(getServiceID(), FUNCTION_SUBCODES, requestID, m_socketID, 0, getCompressType(), bytes.length, bytes));
            bw.close();
       }catch(Exception ex){
           
       }
    }

    /*
     *反注册代码
     *@param requestID 请求ID
     *@param codes 代码
     */
    public void unSubCodes(int requestID) {
        FCBinary bw = new FCBinary();
        try{
            bw.writeString("1");
            byte[] bytes = bw.getBytes();
            int ret = send(new FCMessage(getServiceID(), FUNCTION_UNSUBCODES, requestID, m_socketID, 0, getCompressType(), bytes.length, bytes));
            bw.close();
        }catch(Exception ex){
        }
    }

    /*
     * 获取报价数据
     *@param body 包体
     *@param bodyLength 包体长度
     */
    public static ArrayList<PriceData> getPriceDatas(byte[] body, int bodyLength) {     
        ArrayList<PriceData> priceDatas = new ArrayList<PriceData>();
        try{
            FCBinary br = new FCBinary();
            br.write(body, bodyLength);
            if (body != null && body.length > 0)
            {
                int datasSize = br.readInt();
                for (int i = 0; i < datasSize; i++)
                {
                    PriceData data = new PriceData();
                    data.m_code = br.readString();
                    data.m_lastClose = br.readFloat();
                    data.m_close = br.readFloat();
                    int type = br.readByte();
                    if (type == 0)
                    {
                        data.m_volume = br.readDouble();
                        data.m_amount = br.readDouble();
                    }
                    else
                    {
                        data.m_volume = br.readInt();
                        data.m_amount = br.readInt();
                    }
                    data.m_high = br.readFloat();
                    data.m_low = br.readFloat(); 
                    data.m_upperLimit = br.readFloat();
                    data.m_lowerLimit = br.readFloat();
                    data.m_ratio = br.readFloat();
                    data.m_pe = br.readFloat();
                    data.m_turnoverRate = br.readFloat();
                    data.m_marketValue = br.readFloat();
                    data.m_flow = br.readFloat();
                    data.m_speed = br.readFloat();
                    priceDatas.add(data);
                }
            }
            br.close();
        }catch(Exception ex)
        {
            
        }
        return priceDatas;
    }

    /*
     * 接收消息方法
     *@param message 消息
     */
    public void onReceive(FCMessage message) {
        super.onReceive(message);
        sendToListener(message);
    }
}

